<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="Swagger UI入门部署"><meta name="keywords" content="Swagger"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>Swagger UI入门部署 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#概述："><span class="toc-number">1.</span> <span class="toc-text">概述：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#资源传送门"><span class="toc-number">2.</span> <span class="toc-text">资源传送门</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#安装部署"><span class="toc-number">3.</span> <span class="toc-text">安装部署</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-从-git-上-clone"><span class="toc-number">3.1.</span> <span class="toc-text">1.从 git 上 clone</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-新建一个文件夹-public"><span class="toc-number">3.2.</span> <span class="toc-text">2.新建一个文件夹 public</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-在-IIS-上部署"><span class="toc-number">3.3.</span> <span class="toc-text">3.在 IIS 上部署</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-访问"><span class="toc-number">3.4.</span> <span class="toc-text">4.访问</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#部署自己的接口-API"><span class="toc-number">4.</span> <span class="toc-text">部署自己的接口 API</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#步骤："><span class="toc-number">4.0.1.</span> <span class="toc-text">步骤：</span></a></li></ol></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">Swagger UI入门部署</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-01-09</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/Swagger/">Swagger</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h3 id="概述："><a href="#概述：" class="headerlink" title="概述："></a>概述：</h3><p>在开发过程中，通用的功能我们通常会定义一些接口，但随着项目越做越大，时间越来越久，接口就越来越多，忘的就越来越多，沟通成本就越来越高，最后就会想到，当时把定义的接口及用法都写下就好了，巴拉巴拉一大堆后悔的话。</p>
<p><strong>为了解决上述描述的问题，Swagger UI 应运而生。</strong></p>
<p>Swagger UI 可以让我们把定义的接口以配置文件的方式编写，最后生成一个可视化较高的网站。</p>
<a id="more"></a>

<p><strong>Swagger UI 有如下优势：</strong></p>
<ul>
<li>编写语言统一</li>
<li>风格统一<br>不会像自己写的 word 说明文档，各成一家</li>
<li>可读性高<br>生成的界面简洁、信息全</li>
<li>可交互<br>这是最可贵的，根据查看到的接口，我们不仅能看到接口的链接串、参数、返回值等各种信息，我们还可以真是的对服务器进行请求，查看真实的交互，大大减小了同事之间的交互成本。</li>
</ul>
<h3 id="资源传送门"><a href="#资源传送门" class="headerlink" title="资源传送门"></a>资源传送门</h3><p><a href="http://swagger.io/specification/" target="_blank" rel="noopener">官网地址</a><br><a href="http://petstore.swagger.io/" target="_blank" rel="noopener">Demo 地址</a><br><a href="http://editor.swagger.io/#/" target="_blank" rel="noopener">在线编辑地址</a></p>
<blockquote>
<p>可以把 Swagger UI 理解成自己部署的一个网站</p>
</blockquote>
<h3 id="安装部署"><a href="#安装部署" class="headerlink" title="安装部署"></a>安装部署</h3><p>下面介绍下如何部署自己的 Swagger UI</p>
<h4 id="1-从-git-上-clone"><a href="#1-从-git-上-clone" class="headerlink" title="1.从 git 上 clone"></a>1.从 git 上 clone</h4><p>github 上 clone：<a href="https://github.com/swagger-api/swagger-ui" target="_blank" rel="noopener">https://github.com/swagger-api/swagger-ui</a></p>
<h4 id="2-新建一个文件夹-public"><a href="#2-新建一个文件夹-public" class="headerlink" title="2.新建一个文件夹 public"></a>2.新建一个文件夹 public</h4><p>将下载到的 swagger ui 里的 dist 文件夹里的文件复制到 public 文件夹里</p>
<h4 id="3-在-IIS-上部署"><a href="#3-在-IIS-上部署" class="headerlink" title="3.在 IIS 上部署"></a>3.在 IIS 上部署</h4><p>在运行中 inetmgr 打开 IIS，把 public 文件夹新建一个网站<br>如果 MIME Types 中没有对 json 的支持，需要添加 .json、application/json</p>
<h4 id="4-访问"><a href="#4-访问" class="headerlink" title="4.访问"></a>4.访问</h4><p>在浏览器中输入你配置的网址就可以访问了<br><a href="http://localhost:8011/index.html" target="_blank" rel="noopener">http://localhost:8011/index.html</a></p>
<h3 id="部署自己的接口-API"><a href="#部署自己的接口-API" class="headerlink" title="部署自己的接口 API"></a>部署自己的接口 API</h3><p>上面的部署的接口来源是从 Swagger UI 官网提供的，如何部署自己的接口 API 呢，这需要先编写自己的 API 代码。<br><a href="http://editor.swagger.io/#/" target="_blank" rel="noopener">Swagger Editor</a>提供了编写 Swagger UI 的工具。<br>大家可以在这里编写自己的 Swagger UI，该框架使用的是 YAML 语言进行编写，不熟悉的朋友可以自行学习下。</p>
<h5 id="步骤："><a href="#步骤：" class="headerlink" title="步骤："></a>步骤：</h5><ul>
<li>在 Swagger Editor 中点击 File—&gt;Download JSON</li>
<li>将下载的 json 文件你的 public 文件夹下</li>
<li>修改 index.html 文件，将原来引用官网 json 文件的链接改成刚生成的 json 文件的路径</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">  <span class="comment">// Build a system</span></span><br><span class="line">  <span class="keyword">const</span> ui = SwaggerUIBundle(&#123;</span><br><span class="line">    url: <span class="string">"./swagger.json"</span>,</span><br><span class="line">    dom_id: <span class="string">'#swagger-ui'</span>,</span><br><span class="line">    presets: [</span><br><span class="line">      SwaggerUIBundle.presets.apis,</span><br><span class="line">      SwaggerUIStandalonePreset</span><br><span class="line">    ],</span><br><span class="line">    plugins: [</span><br><span class="line">      SwaggerUIBundle.plugins.DownloadUrl</span><br><span class="line">    ],</span><br><span class="line">    layout: <span class="string">"StandaloneLayout"</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="built_in">window</span>.ui = ui</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在浏览器中访问下，能访问就 OK 了。</p>
<p>推荐一个基于 node 部署的<a href="http://www.jianshu.com/p/d6626e6bd72c" target="_blank" rel="noopener">链接</a></p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/01/09/Swagger-UI%E5%85%A5%E9%97%A8%E9%83%A8%E7%BD%B2/">http://blog.wangpengpeng.site/2020/01/09/Swagger-UI%E5%85%A5%E9%97%A8%E9%83%A8%E7%BD%B2/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Swagger/">Swagger</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/01/09/Swagger-UI%E5%90%84%E5%8F%82%E6%95%B0%E8%AE%B2%E8%A7%A3/"><i class="fa fa-chevron-left">  </i><span>Swagger UI各参数讲解</span></a></div><div class="next-post pull-right"><a href="/2020/01/09/reactjs%E4%B8%AD%E4%BA%8B%E4%BB%B6%E4%BC%A0%E5%8F%82/"><span>reactjs中事件传参</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>